package MapHomework;

import java.util.*;

public class Text {
    public static void main(String[] args) {
        Map<Integer, String> map = new HashMap<Integer, String>();
        map.put(1, "Amy");
        map.put(2, "Joe");
        map.put(3, "Tom");
        map.put(4, "Susan");

        //1.遍历集合，并将序号与对应人名打印。
        Set set = map.keySet();
        for (Object key : set) {
            System.out.println(key + " " + map.get(key));
        }

        //2.向该map集合中插入一个编码为5姓名为控制台输入的人名的信息
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入第5个人的名字");
        map.put(5, scan.next());

        //3.移除该map中的编号为4的信息
        map.remove(4);

        //4.将map集合中编号为3的姓名信息修改为"Tommy"
        boolean result = map.containsKey(3);
        if (result == true) {
            map.put(3, "Tommy");
        }

        //5.再次遍历经过上述操作后的集合，并将序号与对应人名打印。(注：第一次输出和第二次输出需用空行隔开)
        Set sets = map.keySet();
        for (Object key : sets) {
            System.out.println(key + " " + map.get(key));
        }

        //6. 给定["a", "b", "a", "b", "c", "a", "b", "c", "b"]字符串数组，然后使用Map的key来保存数组中字符串元素，value保存该字符串元素的出现次数，最后统计出各字符串元素的出现次数。
        Map<String,Integer > map1 = new HashMap();
        List<String> list = Arrays.asList("a", "b", "a", "b", "c", "a", "b", "c", "b");
        HashSet<String> hashSet = new HashSet();
        hashSet.addAll(list);
        for (String obj : hashSet) {
            int n = 0;
            for(int j=0;j<list.size();j++){
                if(obj.equals(list.get(j))){
                    n++;
                }
                map1.put(obj, n);
            }
        }
        Set set1=map1.keySet();
        for(Object obj1:set1){
            System.out.println(obj1+""+map1.get(obj1));
        }
    }
}
